/**
 * 自定义订单
 * */


var formArys = [];
var pages = {
    start: 1, limit: 5, total: 0,
    url: '/admin/order.html'
};
pages.start = getParamByUrl('start');
pages.start = pages.start ? pages.start : 1;


(function () {
    getList();
})();


//购物车列表
function getList(){
    $.ajax({
        type: apis.orderIndex.method,
        url: apis.orderIndex.url,
        data: { start: pages.start, limit: pages.limit },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            formArys = res.data;
            $('#pageTotal').html(formArys.length);
            $('tbody').html(getHtml());
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//拼接字符串
function getHtml(){
    var d = formArys;
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].uname + '</td>';
        h += '<td>' + d[i].gname + '</td>';
        h += '<td><img src="' + d[i].gimg + '" height="100"/></td>';
        h += '<td>' + d[i].sku_name + '</td>';
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].num + '</td>';
        h += '<td>' + d[i].real_pay + '</td>';
        h += '<td class="statusDiv">';
        h += '<div>下单时间：' + d[i].addtime + '</div>';
        h += '<div>';
        h += '当前状态：' + d[i].status_name;
        h += '</div>';
        h += orderStatus();
        h += '</td>';
        //h += '<td>';
        //if (d[i].iscancel == 3) {
        //    h += '<br/><span class="status no">已取消</span>';
        //} else {
        //    h += d[i].cancel_name;
        //}
        //h += '</td>';
        //h += '<td class="f-14">';
        //if (d[i].iscancel == 2) {
        //    h += '<a href="javascript:;" class="btn" onclick="order_shenhe(' + d[i].id + ');">';
        //    h += '通过审核';
        //    h += '</a>';
        //} else {
        //}
        //if (d[i].status == 4) {
        //    h += '<div style="height: 5px;"></div>';
        //    h += '<a href="javascript:;" class="btn" onclick="order_kefu(' + d[i].id + ');">';
        //    h += '设置完成';
        //    h += '</a>';
        //}
        //if (d[i].status < 4 && d[i].iscancel != 2) {
        //    h += '-';
        //}
        //h += '<a href="javascript:;" onclick="brand_show(\'编辑\',\'brand-show.html\', ' + d[i].id + ');" style="text-decoration:none">';
        //h += '详情<i class="Hui-iconfont">&#xe6df;</i>';
        //h += '</a>';
        //h += ' &nbsp; ';
        //h += '<a href="javascript:;" onclick="brand_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        //h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        //h += '</a>';
        //h += '</td>';
        h += '</tr>';
    }
    return h;
}
//订单状态
function orderStatus(){
    var h = '';
    //h += '<div>订单状态：';
    //if (d[i].status < 3) {
    //    h += '<span class="status">未付款</span>';
    //} else {
    //    h += '<span class="status yes">已付款</span>';
    //    h += d[i].paytime;
    //}
    //h += '</div>';
    //h += '<div>发货状态：';
    //if (d[i].status < 4) {
    //    h += '<span class="status">未发货</span>';
    //} else {
    //    h += '<span class="status yes">已发货</span>';
    //    h += d[i].sendtime;
    //}
    //h += '</div>';
    //h += '<div>收货状态：';
    //if (d[i].status < 5) {
    //    h += '<span class="status">未收货</span>';
    //} else {
    //    h += '<span class="status yes">已发收货</span>';
    //    h += d[i].receivetime;
    //}
    //h += '</div>';
    //h += '<div>订单状态：';
    //if (d[i].status == 2) {
    //    h += '<span class="status no">' + d[i].status_name + '</span>';
    //} else if (d[i].status == 5) {
    //    h += '<span class="status yes">' + d[i].status_name + '</span>';
    //} else {
    //    h += '<span class="status jindu">' + d[i].status_name + '</span>';
    //}
    //h += '</div>';
    h += '<style type="text/css"> .otab th, .otab td { padding: 2px 10px; padding-top: 5px; border: 1px solid #eee;  } .otab th.oclick, .otab td.oclick { border: 1px solid #0088cc; } .otab div { margin-bottom: 5px; color: #fff; background: #ccc; text-align: center; cursor: pointer; } .otab div:hover { background: #555; color: #eee; } </style>';
    h += '<table class="otab">';
    h += '<tr>';
    h += '<th class="oclick">关闭</th>';
    h += '<th>快递</th>';
    h += '<th>自提</th>';
    h += '</tr>';
    h += '<tr>';
    h += '<td valign="top" class="oclick">';
    h += '<div>正常</div>';
    h += '<div>审核中</div>';
    h += '<div>售后</div>';
    h += '</td>';
    h += '<td>';
    h += '<div>未付款</div>';
    h += '<div>未发货</div>';
    h += '<div>未收货</div>';
    h += '<div>已完成</div>';
    h += '</td>';
    h += '<td valign="top">';
    h += '<div>未付款</div>';
    h += '<div>门店自提</div>';
    h += '<div>自提成功</div>';
    h += '</td>';
    h += '</tr>';
    h += '</table>';
    return h;
}


//审核弹窗
function order_shenhe(v){
    var msg = '确认要 <b style="color: #0088CC; font-size: 20px;">通过审核</b> 吗？';
    layer.confirm(msg,function(index){
        //$(obj).parents("tr").remove();
        //console.log(id, index);
        $.ajax({
            type: apis.orderAuth.method,
            url: apis.orderAuth.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg);
                    return
                }
                layerMsgOk('该模板已通过审核');
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//售后弹窗
function order_kefu(v){
    var msg = '确认要 <b style="color: #0088CC; font-size: 20px;">完成</b> 吗？';
    layer.confirm(msg,function(index){
        //$(obj).parents("tr").remove();
        //console.log(id, index);
        $.ajax({
            type: apis.orderKefu.method,
            url: apis.orderKefu.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg);
                    return
                }
                layerMsgOk('该模板已完成');
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}